<?php

namespace app\api\service;

use app\facade\Basic;
use app\common\RedisClient;
use app\api\logic\CacheLogic;

class Index
{

    protected $refresh = false;

    public function __construct()
    {
        $this->refresh = env('ENV_REFRESH_CACHE', false);
    }

    public function complex($lang)
    {
        $cacheLogic = new CacheLogic();
        $hotMiners = $cacheLogic->getHotMiners($this->refresh);
        $topMiners = $cacheLogic->getTopMiners($this->refresh);
        $hotData = [];
        $topData = [];

        foreach ($hotMiners ?? [] as $key => &$value) {
            if ($value['inventory_type'] == -1 && !$value['max_price']) {
                $value['max_price'] = $value['estimate_price'];
            }
            if ($value['inventory_type'] == -1 && !$value['min_price']) {
                $value['min_price'] = $value['estimate_price'];
            }
            if ($value['inventory_type'] == -1 && !$value['max_first_discount_price']) {
                $value['max_first_discount_price'] = $value['estimate_price'];
            }
            if ($value['inventory_type'] == -1 && !$value['min_first_discount_price']) {
                $value['min_first_discount_price'] = $value['estimate_price'];
            }
            // 定义基础产品数据结构
            $hotData[] = [
                'id' => $value['id'],
                'name' => $value['productname'],
                'urlname' => $value['producturlname'],
                'image' => $value['main_photo'][0]['url'] ?? '',
                'max_price' => $value['max_price'] ?? '',
                'min_price' => $value['min_price'] ?? '',
                'max_first_discount_price' => $value['max_first_discount_price'] ?: $value['max_price'] ?? '',
                'min_first_discount_price' => $value['min_first_discount_price'] ?: $value['min_price'] ?? '',
                'hashrate' => $value['hashrate'] ?? '',
                'power' => $value['power'] ?? '',
                'inventory_type' => $value['inventory_type'] ?? '',
                'inventory_type_text' => $value['inventory_type_text'] ?? '',
                'describe' => $value['describe'] ?? '',
                'type' => (int)$value['type'] ?? '',
                'primary_brand' => $value['primary_brand'] ?? '--',
            ];
        }

        foreach ($topMiners ?? [] as $key => &$value) {
            if ($value['inventory_type'] == -1 && !$value['max_price']) {
                $value['max_price'] = $value['estimate_price'];
            }
            if ($value['inventory_type'] == -1 && !$value['min_price']) {
                $value['min_price'] = $value['estimate_price'];
            }
            if ($value['inventory_type'] == -1 && !$value['max_first_discount_price']) {
                $value['max_first_discount_price'] = $value['estimate_price'];
            }
            if ($value['inventory_type'] == -1 && !$value['min_first_discount_price']) {
                $value['min_first_discount_price'] = $value['estimate_price'];
            }
            // 定义基础产品数据结构
            $topData[] = [
                'id' => $value['id'],
                'name' => $value['productname'],
                'urlname' => $value['producturlname'],
                'image' => $value['main_photo'][0]['url'] ?? '',
                'max_price' => $value['max_price'] ?? '',
                'min_price' => $value['min_price'] ?? '',
                'max_first_discount_price' => $value['max_first_discount_price'] ?: $value['max_price'] ?? '',
                'min_first_discount_price' => $value['min_first_discount_price'] ?: $value['min_price'] ?? '',
                'hashrate' => $value['hashrate'] ?? '',
                'power' => $value['power'] ?? '',
                'inventory_type' => $value['inventory_type'] ?? '',
                'inventory_type_text' => $value['inventory_type_text'] ?? '',
                'describe' => $value['describe'] ?? '',
                'type' => (int)$value['type'] ?? '',
                'primary_brand' => $value['primary_brand'] ?? '--',
            ];
        }

        if ($hotData) {
            $hotData = translate($hotData, $lang, ['name', 'describe', 'inventory_type_text'], ['name', 'describe', 'inventory_type_text']);
        }
        if ($topData) {
            $topData = translate($topData, $lang, ['name', 'describe', 'inventory_type_text'], ['name', 'describe', 'inventory_type_text']);
        }

        $result = [
            'hot_miners' => $hotData ?? [],
            'top_miners' => $topData ?? [],
        ];
        return $result;
    }

    public function publicCategories($lang)
    {
        $result = [];
        $redis = new RedisClient();
        $redis = $redis->getClient();
        $cacheLogic = new CacheLogic();

        $brands = $cacheLogic->getAllBrands($lang, $this->refresh, 'asc');
        $algorithms = $cacheLogic->getAllAlgorithms($lang, $this->refresh);
        $coins = $cacheLogic->getAllCoins($lang, $this->refresh, 'asc');
        $accessory = $cacheLogic->getAllAccessory(0, $lang, $this->refresh);
        $solution = $cacheLogic->getAllSolution($lang, $this->refresh);

        $result = [
            'brands' => $brands ?? [],
            'coins' => $coins ?? [],
            'algorithms' => $algorithms ?? [],
            'accessory' => $accessory ?? [],
            'solution' => $solution ?? [],
        ];
        return $result;
    }


    /**
     * @describe
     * @author: Bobo
     * @date: 2025-04-24 13:09:51 
     * @param [type] @required
     * @return Renderable
     */
    public function clearCache($type)
    {
        $redis = new RedisClient();
        $redis = $redis->getClient();
        $cacheLogic = new CacheLogic();
        return $cacheLogic->clearCache($type);
    }
}
